<template>
	<view class="container">
		<view class="article-header">
			<text class="title">{{ article.title }}</text>
			<view class="meta">
				<view class="meta-left">
					<text class="author">{{ article.author }}</text>
					<text class="dot">·</text>
					<text class="time">{{ dateTimeFormat(article.createTime) }}</text>
				</view>
				<view class="actions">
					<view class="action-item" @click="handleLike">
						<uni-icons :type="isLiked ? 'heart-filled' : 'heart'" size="20"
							:color="isLiked ? '#ff4444' : '#999'">
						</uni-icons>
						<text>{{ article.likeCount }}</text>
					</view>
				</view>
			</view>
		</view>

		<image v-if="article.coverImage" class="cover-image" :src="article.coverImage" mode="widthFix"></image>

		<view class="article-content">
			<text>{{ article.content }}</text>
		</view>

		<!-- 评论区 -->
		<view class="comments-section">
			<view class="section-title">评论 ({{ comments.length }})</view>

			<!-- 评论输入框 -->
			<view class="comment-input">
				<input v-model="newComment" placeholder="写下你的评论..." @confirm="submitComment" class="input" />
				<button class="submit-btn" @click="submitComment" :disabled="!newComment.trim()">发送</button>
			</view>

			<!-- 评论列表 -->
			<view class="comments-list">
				<view v-if="comments.length === 0" class="no-comments">
					暂无评论，快来抢沙发吧~
				</view>
				<view v-else v-for="comment in comments" :key="comment.id" class="comment-item">
					<view class="comment-header">
						<view class="user-info">
							<image class="avatar" :src="comment.avatarUrl" mode="aspectFill"></image>
							<text class="username">{{ comment.nickname }}</text>
						</view>
					</view>
					<text class="content">{{ comment.content }}</text>
					<view class="comment-footer">
						<text class="time">{{ dateTimeFormat(comment.createTime) }}</text>
						<view class="comment-actions">
							<view class="action-item" @click.stop="handleCommentLike(comment)">
								<uni-icons 
									:type="comment.isLiked ? 'heart-filled' : 'heart'" 
									size="14" 
									:color="comment.isLiked ? '#ff4444' : '#999'"
								></uni-icons>
								<text :class="{ 'liked': comment.isLiked }">{{ comment.likeCount || 0 }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request.js'
import { dateTimeFormat } from '@/utils/system'
import { onLoad } from '@dcloudio/uni-app'

const article = ref({})
const isLiked = ref(false)
const comments = ref([])
const newComment = ref('')

const getArticleDetail = async (id) => {
	try {
		const res = await request.get(`/article/detail/${id}`)
		article.value = res.data
		await request.post(`/article/views/${id}`)
		checkLikeStatus()
	} catch (e) {
		uni.showToast({
			title: e || '获取文章详情失败',
			icon: 'none'
			
		})
	}
}

const handleLike = async () => {
	try {
		// 后端修改喜欢状态
		const res = await request.post(`/article/like/${article.value.id}`)
		// 前端修改喜欢状态
		isLiked.value = res.data;
		if (res.data) {
			article.value.likeCount++
		} else {
			article.value.likeCount--
		}
	} catch (e) {
		uni.showToast({
			title: '操作失败',
			icon: 'none'
		})
	}
}

const checkLikeStatus = async () => {
	try {
		const res = await request.get(`/article/like/check/${article.value.id}`)
		isLiked.value = res.data
	} catch (e) {
		console.error(e)
	}
}

// 获取评论列表
const getComments = async () => {
	try {
		console.log(article)
		const res = await request.get(`/comment/${article.value.id}`)
		comments.value = res.data
	} catch (e) {
		console.error('获取评论失败:', e)
	}
}

// 提交评论
const submitComment = async () => {
	if (!newComment.value.trim()) return

	try {
		await request.post(`/comment`, {
			articleId: article.value.id,
			content: newComment.value
		})

		// 清空输入框并刷新评论列表
		newComment.value = ''
		await getComments()

		uni.showToast({
			title: '评论成功',
			icon: 'success'
		})
	} catch (e) {
		uni.showToast({
			title: e || '评论失败',
			icon: 'none'
		})
	}
}

// 从路由参数中获取文章ID
const getArticleId = () => {
	const pages = getCurrentPages()
	const currentPage = pages[pages.length - 1]
	return currentPage.options.id
}

// 处理评论点赞的方法
const handleCommentLike = async (comment) => {
	try {
		const res = await request.post(`/comment/like/${comment.id}`)
		// 更新评论的点赞状态
		comment.isLiked = res.data
		if (res.data) {
			comment.likeCount++
		} else {
			comment.likeCount--
		}
	} catch (e) {
		uni.showToast({
			title: e || '操作失败',
			icon: 'none'
		})
	}
}

onLoad(async (option) => {
	const id = option.id
	if (id) {
		await getArticleDetail(id)
		getComments() // 获取评论列表
	}
})

// onMounted(async () => {
// 	const id = getArticleId()
// 	if (id) {
// 		await getArticleDetail(id)
// 		getComments() // 获取评论列表
// 	}
// })
</script>

<style lang="scss" scoped>
.container {
	padding: 30rpx;
}

.article-header {
	margin-bottom: 40rpx;
	position: relative;

	.title {
		font-size: 40rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		padding-bottom: 20rpx;
		display: block;
		border-bottom: 1px solid #eee;
	}

	.meta {
		display: flex;
		justify-content: space-between;
		align-items: center;

		.meta-left {
			display: flex;
			align-items: center;
			gap: 10rpx;

			.author {
				font-size: 28rpx;
				color: #333;
				font-weight: bold;
			}

			.dot {
				color: #999;
				font-size: 24rpx;
			}

			.time {
				font-size: 24rpx;
				color: #999;
			}
		}

		.actions {
			display: flex;
			gap: 30rpx;

			.action-item {
				display: flex;
				align-items: center;
				gap: 6rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}
}

.cover-image {
	width: 100%;
	border-radius: 16rpx;
	margin-bottom: 30rpx;
}

.article-content {
	font-size: 30rpx;
	line-height: 1.8;
	color: #333;
}

.comments-section {
	margin-top: 40rpx;
	padding-top: 30rpx;
	border-top: 1px solid #eee;

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
	}

	.comment-input {
		display: flex;
		gap: 20rpx;
		margin-bottom: 30rpx;

		.input {
			flex: 1;
			height: 70rpx;
			background: #f5f5f5;
			border-radius: 35rpx;
			padding: 0 30rpx;
			font-size: 28rpx;
		}

		.submit-btn {
			width: 140rpx;
			height: 70rpx;
			background: #36D1DC;
			color: #fff;
			border-radius: 35rpx;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 0;

			&:disabled {
				opacity: 0.6;
			}
		}
	}

	.comments-list {
		.no-comments {
			text-align: center;
			color: #999;
			font-size: 28rpx;
			padding: 40rpx 0;
		}

		.comment-item {
			padding: 16rpx 20rpx;
			margin-bottom: 16rpx;
			background: rgba(255, 255, 255, 0.8);
			border-radius: 16rpx;
			backdrop-filter: blur(10px);
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
			transition: all 0.3s ease;

			&:hover {
				box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
				transform: translateY(-2rpx);
			}

			.comment-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 6rpx;

				.user-info {
					display: flex;
					align-items: center;
					gap: 8rpx;

					.avatar {
						width: 40rpx;
						height: 40rpx;
						border-radius: 50%;
						border: 2rpx solid #fff;
						box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
					}

					.username {
						font-size: 24rpx;
						color: #333;
						font-weight: bold;
						background: linear-gradient(45deg, #333, #666);
						-webkit-background-clip: text;
						-webkit-text-fill-color: transparent;
					}
				}
			}

			.content {
				padding: 4rpx 0;
				font-size: 26rpx;
				color: #666;
				line-height: 1.5;
			}

			.comment-footer {
				padding-top: 10rpx;
				border-top: 1rpx solid rgba(0, 0, 0, 0.03);
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				.time {
					font-size: 20rpx;
					color: #999;
				}
				
				.comment-actions {
					.action-item {
						display: flex;
						align-items: center;
						gap: 4rpx;
						padding: 4rpx 12rpx;
						background: rgba(0, 0, 0, 0.02);
						border-radius: 30rpx;
						transition: all 0.3s ease;

						&:active {
							transform: scale(0.95);
						}

						text {
							font-size: 20rpx;
							color: #999;
							
							&.liked {
								color: #ff6b6b;
							}
						}
					}
				}
			}
		}
	}
}
</style>